@echo off

python -c "from version import version; f = open('dummy','w'); f.write(version); f.close();"

set /P VERSION=<dummy
del dummy

if "%1" == "clean" goto clean

mkdir "build-%VERSION%\notewhal-%VERSION%_src"
mkdir "build-%VERSION%\notewhal-%VERSION%_src\data"
mkdir "build-%VERSION%\notewhal-%VERSION%_src\res"
mkdir "build-%VERSION%\notewhal-%VERSION%_src\help"
mkdir "build-%VERSION%\notewhal-%VERSION%_win32_bin"
mkdir "build-%VERSION%\notewhal-%VERSION%_win32_bin\data"

if exist *.chm del *.chm
if exist *.pyc del *.pyc
if exist *.pyo del *.pyo

rem archive source
copy *.* build-%VERSION%\notewhal-%VERSION%_src
del build-%VERSION%\notewhal-%VERSION%_src\.hgignore
del build-%VERSION%\notewhal-%VERSION%_src\.gitignore
copy res\*.* build-%VERSION%\notewhal-%VERSION%_src\res
copy help\*.* build-%VERSION%\notewhal-%VERSION%_src\help
copy data\*.* build-%VERSION%\notewhal-%VERSION%_src\data
if exist build-%VERSION% cd build-%VERSION%
"C:\Program Files\7-Zip\7z.exe" a -tzip notewhal-%VERSION%_src.zip notewhal-%VERSION%_src\*.*
"C:\Program Files\7-Zip\7z.exe" a -tzip notewhal-%VERSION%_src.zip notewhal-%VERSION%_src\res\*.*
"C:\Program Files\7-Zip\7z.exe" a -tzip notewhal-%VERSION%_src.zip notewhal-%VERSION%_src\help\*.*
"C:\Program Files\7-Zip\7z.exe" a -tzip notewhal-%VERSION%_src.zip notewhal-%VERSION%_src\data\*.*
cd..
rd build-%VERSION%\notewhal-%VERSION%_src /Q /S


rem build CHM
"C:\Program Files\HTML Help Workshop\hhc.exe" help\notewhal.hhp
rem move chm
copy notewhal.chm build-%VERSION%\notewhal-%VERSION%_win32_bin

rem build win32 binary
python c:\tools\pyinstaller\Makespec.py --windowed --icon=res/icon.ico --name=notewhal notewhal.py
python c:\tools\pyinstaller\Build.py notewhal.spec
rem copy files
copy license.txt dist\notewhal\license.txt
copy notewhal.chm dist\notewhal\notewhal.chm
copy dist\notewhal\*.* build-%VERSION%\notewhal-%VERSION%_win32_bin
copy data\*.* build-%VERSION%\notewhal-%VERSION%_win32_bin\data

rem archive files
if exist build-%VERSION% cd build-%VERSION%
"C:\Program Files\7-Zip\7z.exe" a -tzip notewhal-%VERSION%_win32_bin.zip notewhal-%VERSION%_win32_bin\*.*
"C:\Program Files\7-Zip\7z.exe" a -tzip notewhal-%VERSION%_win32_bin.zip notewhal-%VERSION%_win32_bin\data\*.*
cd..
if exist build-%VERSION%\notewhal-%VERSION%_win32_bin rd build-%VERSION%\notewhal-%VERSION%_win32_bin /Q /S

rem build installer
"C:/Program Files/NSIS/makensis.exe" /DPRODUCT_VERSION=%VERSION% win32_setup.nsi
rem move installer
if exist notewhal-%VERSION%_win32_setup.exe move notewhal-%VERSION%_win32_setup.exe build-%VERSION%

if exist build rd build /S /Q
if exist dist rd dist /S /Q
if exist notewhal rd notewhal /S /Q
if exist *.chm del *.chm
if exist *.pyc del *.pyc
if exist *.pyo del *.pyo
if exist *.spec del *.spec
if exist warnnotewhal.txt del warnnotewhal.txt
if exist *.log del *.log

goto end

:clean

if exist build-%VERSION% rd build-%VERSION% /S /Q
if exist build rd build /S /Q
if exist dist rd dist /S /Q
if exist notewhal rd notewhal /S /Q
if exist *.chm del *.chm
if exist *.pyc del *.pyc
if exist *.pyo del *.pyo
if exist *.spec del *.spec
if exist warnnotewhal.txt del warnnotewhal.txt
if exist *.log del *.log

:end